package com.echo.servlet;
import java.awt.Font;
import java.awt.RenderingHints;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.jfree.chart.*;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class ChartTest {
	
	ChartTest(){
		;
	}
	
	/*
	public CategoryDataset createMyDataSet(ArrayList<ArrayList<Float>> wholeList, int docNum, int termNum){
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		for (int i = 0; i < docNum; i++){
			Integer docNo = new Integer(i);
			ArrayList<Float> tempList = wholeList.get(i);
			for (int j = 0; j < tempList.size(); j++){
				Integer termNo = new Integer(j);
				dataset.addValue(tempList.get(j), termNo.toString(), docNo.toString());
			}
		}
		return dataset;
	}*/
	
	// CALL this one! To draw a chart for one document
	public void drawSingleChart(ArrayList<Float> ratioList, ArrayList<String> termList, float thold, String restID)
	{
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();
		for (int i = 0; i < termList.size(); i++){
			if(ratioList.get(i) >= thold){
				dataset.addValue(ratioList.get(i), termList.get(i), restID);
				dataset2.addValue(ratioList.get(i), termList.get(i), restID);
			}
			else{
				dataset2.addValue(0, termList.get(i), restID);
			}
		}
		
		JFreeChart chart =  ChartFactory.createBarChart3D(
				"Histogram of " + restID,
				"Term",
				"Term Frequency Ratio",
				dataset,
				PlotOrientation.VERTICAL,
				true,
				false,
				false);
			
		Font font = new Font("Arial", Font.HANGING_BASELINE, 20);
		TextTitle title = new TextTitle("Histogram Test");
		LegendTitle legend = chart.getLegend();
		legend.setItemFont(new Font("Arial", Font.BOLD, 16));
		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		CategoryAxis cateaxis = plot.getDomainAxis();
		cateaxis.setLabelFont(new Font("Arial", Font.BOLD, 16));
		cateaxis.setTickLabelFont(new Font("Arial", Font.BOLD, 16));
		NumberAxis numaxis = (NumberAxis) plot.getRangeAxis();
		numaxis.setLabelFont(new Font("Arial", Font.BOLD, 16));
		chart.setTitle(title);
		
		FileOutputStream fosJpg = null;
		try{
			String address = "d:\\"+restID+".jpg";
//			fosJpg = new FileOutputStream("D:\\test.jpg");
			fosJpg = new FileOutputStream(address);
			ChartUtilities.writeChartAsJPEG(fosJpg,1,chart, 480, 320,null);
			//ChartUtilities.writeChartAsJPEG(out, quality, chart, width, height);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try{
				fosJpg.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		System.out.println("drawing is successful!");
		
		JFreeChart chart2 =  ChartFactory.createBarChart3D(
				"Histogram of " + restID,
				"Term",
				"Term Frequency Ratio",
				dataset2,
				PlotOrientation.VERTICAL,
				true,
				false,
				false);
			
		Font font2 = new Font("Arial", Font.HANGING_BASELINE, 20);
		TextTitle title2 = new TextTitle("Histogram Test");
		LegendTitle legend2 = chart.getLegend();
		legend2.setItemFont(new Font("Arial", Font.BOLD, 16));
		CategoryPlot plot2 = (CategoryPlot) chart.getPlot();
		CategoryAxis cateaxis2 = plot2.getDomainAxis();
		cateaxis2.setLabelFont(new Font("Arial", Font.BOLD, 16));
		cateaxis2.setTickLabelFont(new Font("Arial", Font.BOLD, 16));
		NumberAxis numaxis2 = (NumberAxis) plot2.getRangeAxis();
		numaxis2.setLabelFont(new Font("Arial", Font.BOLD, 16));
		chart2.setTitle(title2);
		
		FileOutputStream fosJpg2 = null;
		try{
			String address = "d:\\"+restID+"_2.jpg";
//			fosJpg = new FileOutputStream("D:\\test.jpg");
			fosJpg2 = new FileOutputStream(address);
			ChartUtilities.writeChartAsJPEG(fosJpg2,1,chart2, 480, 320,null);
			//ChartUtilities.writeChartAsJPEG(out, quality, chart, width, height);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try{
				fosJpg2.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		System.out.println("drawing is successful!");
		
	}
	
	/*
	public void drawChart(ArrayList<ArrayList<Float>> wholeList, int docNum, int termNum){
		CategoryDataset dataset = this.createMyDataSet(wholeList, docNum, termNum);
		JFreeChart chart =  ChartFactory.createBarChart3D(
				"Histogram Test",
				"Key",
				"Value",
				dataset,
				PlotOrientation.VERTICAL,
				true,
				false,
				false
			);
			
		Font font = new Font("Arial", Font.HANGING_BASELINE, 20);
		TextTitle title = new TextTitle("Histogram Test");
		LegendTitle legend = chart.getLegend();
		legend.setItemFont(new Font("Arial", Font.BOLD, 16));
		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		CategoryAxis cateaxis = plot.getDomainAxis();
		cateaxis.setLabelFont(new Font("Arial", Font.BOLD, 16));
		cateaxis.setTickLabelFont(new Font("Arial", Font.BOLD, 16));
		NumberAxis numaxis = (NumberAxis) plot.getRangeAxis();
		numaxis.setLabelFont(new Font("Arial", Font.BOLD, 16));
		chart.setTitle(title);
		
		FileOutputStream fosJpg = null;
		try{
			fosJpg = new FileOutputStream("D:\\test.jpg");
			ChartUtilities.writeChartAsJPEG(fosJpg,1,chart, 1600, 1200,null);
			//ChartUtilities.writeChartAsJPEG(out, quality, chart, width, height);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try{
				fosJpg.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		System.out.println("drawing is successful!");
		
	}*/
	
}
